package com.company.dao.impl;

import com.company.dao.Userdao;
import com.company.entity.User;
import com.company.jdbc.jdbcutils;

import javax.sound.midi.Soundbank;
import java.sql.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Userdaoimpl implements Userdao {
    @Override
    public List<User> findall() throws SQLException {
       Connection connection= jdbcutils.getConnection();
        Statement statement = connection.createStatement();
        String sql = "select* from user";
        ResultSet resultSet = statement.executeQuery(sql);
        List<User> lis  = new ArrayList<User>();
        while(resultSet.next()){
            User user = new User();
            int is_deleted = resultSet.getInt("is_deleted");
            if(is_deleted==0){
               Date create_time =  resultSet.getDate("create_time");
               int id = resultSet.getInt("id");
               int create_by = resultSet.getInt("create_by");
               Date update_time = resultSet.getDate("update_time");
                  int  update_by = resultSet.getInt("update_by") ;
                 String nickname =resultSet.getString("nickname");
                 String username= resultSet.getString("username");
                String password= resultSet.getString("password");
                 int  sex = resultSet.getInt("sex");
                 String email =resultSet.getString("email");
                 String phone_number = resultSet.getString("phone_number");
                int role_id =resultSet.getInt("role_id");
                user.setCreate_by(create_by);
                user.setCreate_time(create_time);
                user.setEmail(email);
                user.setId(id);
                user.setIs_deleted(is_deleted);
                user.setNickname(nickname);
                user.setPassword(password);
                user.setPhone_number(phone_number);
                user.setRole_id(role_id);
                user.setSex(sex);
                user.setUpdate_by(update_by);
                user.setUpdate_time(update_time);
                user.setUsername(username);
                lis.add(user);

            }
        }
        return  lis;

    }

    @Override
    public boolean saveUser(User user) throws SQLException {
        boolean flag = false;
        Connection connection= jdbcutils.getConnection();
        String sql = "insert into user values(null,?,?,?,?,?,?,?,?,?,?,?,? )";
        PreparedStatement preparedStatement= connection.prepareStatement(sql);
        preparedStatement.setObject(1,user.getCreate_time());
        preparedStatement.setObject(2,user.getCreate_by());
        preparedStatement.setObject(3,user.getUpdate_time());
        preparedStatement.setObject(4,user.getUpdate_by());
        preparedStatement.setObject(5,user.getIs_deleted());
        preparedStatement.setObject(6,user.getNickname());
        preparedStatement.setObject(7,user.getUsername());
        preparedStatement.setObject(8,user.getPassword());
        preparedStatement.setObject(9,user.getSex());
        preparedStatement.setObject(10,user.getEmail());
        preparedStatement.setObject(11,user.getPhone_number());
        preparedStatement.setObject(12,user.getRole_id());
        int count = preparedStatement.executeUpdate();
        if(count>0){
            flag = true;
        }

        return flag;
    }

    @Override
    public boolean updateUser(User user) throws SQLException {
        boolean flag = false;
        Connection connection= jdbcutils.getConnection();
        String sql = "update user set username = ?,sex = ?,password = ? ,update_time=?,update_by=? where id =? ";
        PreparedStatement preparedStatement= connection.prepareStatement(sql);
        preparedStatement.setObject(1,user.getUsername());
        preparedStatement.setObject(2,user.getSex());
        preparedStatement.setObject(3,user.getPassword());
        preparedStatement.setObject(4,user.getUpdate_time());
        preparedStatement.setObject(5,user.getUpdate_by());
        preparedStatement.setObject(6,user.getId());
        int count = preparedStatement.executeUpdate();
        if(count>0){
            flag = true;
        }

        return flag;

    }

    @Override
    public boolean delUserid(User user) throws SQLException {
        boolean flag = false;
        Connection connection= jdbcutils.getConnection();
        String sql = "delete from user where id = ?";
        PreparedStatement preparedStatement= connection.prepareStatement(sql);
        preparedStatement.setObject(1,user.getId());
        int count = preparedStatement.executeUpdate();
        if(count>0){
            flag = true;
        }

        return flag;

    }
   public void adduser() throws SQLException {

       Scanner sc = new Scanner(System.in);
       Date date = new Date();


       User user  = new User();
       user.setCreate_time(date);
       user.setUpdate_time(date);
       System.out.println("请输入用户用户创建者：系统角色编号（1-管理员 2-健康管家）");
       user.setCreate_by(sc.nextInt());
       user.setUpdate_by(user.getCreate_by());
       System.out.println("请输入逻辑删除标记（0：显示；1：隐藏）");
       user.setIs_deleted(sc.nextInt());
       System.out.println("请输入真实姓名");
       user.setNickname(sc.next());
       System.out.println("请输入系统账号");
       user.setUsername(sc.next());
       System.out.println("请输入密码");
       user.setPassword(sc.next());
       System.out.println("请输入性别性别（0：女  1：男）");
       user.setSex(sc.nextInt());
       System.out.println("请输入邮箱");
       user.setEmail(sc.next());
       System.out.println("请输入电话号码");
       user.setPhone_number(sc.next());
       System.out.println("请输入系统角色编号（1-管理员 2-健康管家）");
       user.setRole_id(sc.nextInt());
       boolean bl = saveUser(user);
       if(bl){
           System.out.println("添加成功");
       }else {
           System.out.println("添加失败");
       }



   }
   public  void upuser() throws SQLException {

        User user = new User();
        Date date = new Date();

        user.setUpdate_time(date);
       Scanner sc = new Scanner(System.in);
       System.out.println("请输入id");
       user.setId(sc.nextInt());
       System.out.println("请输入用户用户更新者：系统角色编号（1-管理员 2-健康管家）");
       user.setUpdate_by(sc.nextInt());
       System.out.println("请输入更新后性别（0：女  1：男）");
       user.setSex(sc.nextInt());

       System.out.println("请输入更新后系统账号");
       user.setUsername(sc.next());
       System.out.println("请输入更新后密码");
       user.setPassword(sc.next());
       boolean bl = updateUser(user);
       if(bl){
           System.out.println("更新成功");
       }else {
           System.out.println("更新失败");
       }



   }

}
